package face.pay.web.com.applyment.wx;


import face.pay.common.enterprise.service.IWeChatMchUpgradeInfoService;
import face.pay.entity.MchApplymentUpgradeInfo;
import face.pay.entity.UserInfo;
import face.pay.entity.WeChatMchUpgradeInfo;
import face.pay.system.annotation.CurrentUser;
import face.pay.system.exception.BaseException;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/wXmchUpgrade")
public class WxMchUpgradeController {


    @Autowired
    private IWeChatMchUpgradeInfoService groupService;

    @RequestMapping("/index")
    public String index(@CurrentUser UserInfo loginUser, Integer mchUserId, Model model) {

        Integer mcId = mchUserId == null ? loginUser.getId() : mchUserId;

        WeChatMchUpgradeInfo info = groupService.getByUserId(mcId, loginUser.getOemId(), loginUser.getId());

        model.addAttribute("info", info);

        return "agency/enterprise/applyment/wechat_mch_Upgrade_info";
    }


    /**
     * 物料上传
     *
     * @param file
     * @param keyName
     * @param loginUser
     * @return
     * @throws Exception
     */
    @RequestMapping("/wXmchUpgrade")
    @ResponseBody
    public ResultMessage wxUpload(@RequestParam("file") MultipartFile file, WeChatMchUpgradeInfo weChatMchUpgradeInfo,
                                  String keyName, @CurrentUser UserInfo loginUser) throws Exception {
        return groupService.mediaload(weChatMchUpgradeInfo, file.getBytes(), file.getOriginalFilename(), keyName, loginUser);
    }


    @RequestMapping("/edit")
    @ResponseBody
    public ResultMessage editUser(@CurrentUser UserInfo loginUser, WeChatMchUpgradeInfo weChatMchUpgradeInfo) throws BaseException {

        return groupService.modifyInfo(weChatMchUpgradeInfo, loginUser);

    }


    /**
     * 进件提交
     *
     * @param loginUser
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/subinfo")
    public ResultMessage subinfo(WeChatMchUpgradeInfo weChatMchUpgradeInfo, @CurrentUser UserInfo loginUser) throws Exception {
        return groupService.subinfo(weChatMchUpgradeInfo, loginUser);
    }


    /**
     * 小微进件升级记录入口
     *
     * @return
     */
    @RequestMapping("/wx_list")
    public String applyList(@CurrentUser UserInfo loginUser, Integer mchUserId, Model model) {

        model.addAttribute("mchUserId", mchUserId);

        return "agency/enterprise/applyment/WxApplyment_upgradeList";
    }


    /**
     * 查询升级进件列表
     *
     * @param loginUser
     * @param mchUserId
     * @param pageParam
     * @return
     */
    @RequestMapping("/queryData")
    @ResponseBody
    public PageData queryList(@CurrentUser UserInfo loginUser, Integer mchUserId, PageParam pageParam) {

        MchApplymentUpgradeInfo apply = new MchApplymentUpgradeInfo();

        apply.setOemId(loginUser.getOemId());
        apply.setUserId(mchUserId);

        return this.groupService.queryPage(apply, pageParam, loginUser);
    }
}
